package cn.wolfcode.p2p.base.controller;

import cn.wolfcode.p2p.base.service.ILoginInfoService;
import cn.wolfcode.p2p.base.service.ISendVerifyCodeService;
import cn.wolfcode.p2p.base.vo.LoginInfoVO;
import cn.wolfcode.p2p.util.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


@Slf4j
@Controller
public class RegisterController {

    @Autowired
    private ILoginInfoService loginInfoService;

    @Autowired
    private ISendVerifyCodeService codeService;
    /**
     * 注册用户信息
     *
     * @return
     */
    @RequestMapping("/userRegister")
    @ResponseBody
    public JsonResult userRegister(LoginInfoVO loginInfoVO) {
        loginInfoService.userRegister(loginInfoVO);
        return JsonResult.instance();
    }

    /**
     * 注册时发请求校验数据库查询存在的手机号返回true
     *
     * @param username
     * @return 取反值, 前台接收false做处理
     */
    @RequestMapping("/existUsername")
    @ResponseBody
    public boolean existUsername(String username) {
        return !loginInfoService.existUsername(username);
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public JsonResult sendVerifyCode(String phone){
        codeService.sendVerifycode(phone);
        return JsonResult.instance();
    }

}
